package bfa.playlist_importer_exporter;

import java.io.File;
import java.util.ArrayList;


import bfa.playlist_importer_exporter.R;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

	Button buttonExport,buttonImport;
	ListView listView;
	ArrayList<Playlist> lists;
	PlaylistViewAdapter adapter;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        buttonExport=(Button) findViewById(R.id.buttonExport);
        buttonExport.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				ExportPlaylist.exportPlaylists(MainActivity.this,lists);
			}
		});
        
        
        
        buttonImport=(Button) findViewById(R.id.ButtonImport);
        buttonImport.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {				
				Intent intent = new Intent("org.openintents.action.PICK_FILE");				
				startActivityForResult(intent, 1);				
			}
		});                      
        
//        ArrayAdapter<Playlist> adapter = new ArrayAdapter<Playlist>(this,
//                android.R.layout.simple_list_item_1, ExportPlaylist.getPlayLists(this));
//        setListAdapter(adapter);
        
        lists=ExportPlaylist.getPlayLists(this);
        for (Playlist p:lists) {
        	p.selected=true;
        }
        
        
        listView = (ListView) findViewById(R.id.listView1);
        adapter = new PlaylistViewAdapter(this,lists);
        listView.setAdapter(adapter);

    }
    
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                // A contact was picked.  Here we will just display it
                // to the user.            	
            	File f=new File(data.getData().getPath());            	
            	//f=new File("/mnt/sdcard/download/export_playlists.json.txt");
        		ArrayList<Playlist> temp=ImportPlaylist.importPlaylists(MainActivity.this,f);
        		if (temp!=null) {
        			lists.clear();
        			lists.addAll(temp);
        			adapter.notifyDataSetChanged();
        		}
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    
    
}